package ui;

import java.io.IOException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.GUIContext;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.util.Log;

public class ButtonComponent extends AbstractComponent {
	private String buttonText;
	private Rectangle buttonArea;
	private int index;
	private int stateID;
	private int x;
	private int y;
	private boolean isHovered;
	private boolean buttonDown = false;
    private boolean buttonReleased = false;

	public ButtonComponent(GUIContext gc, String buttonText, int buttonIndex, int stateID) {
		super(gc);
		this.buttonText = buttonText;
		this.index = buttonIndex;
		this.stateID = stateID;
		setLocation(gc.getWidth() / 2 - getWidth() / 2, GameMenuState.titleHeight + getHeight() * index - getHeight() / 2 + 100);
		this.buttonArea = new Rectangle(getX(), getY(), getWidth(), getHeight());
	}

	@Override
	public void render(GUIContext gc, Graphics g) throws SlickException {
		g.setFont(gc.getDefaultFont());
		setLocation(gc.getWidth() / 2 - getWidth() / 2, GameMenuState.titleHeight + getHeight() * index - getHeight() / 2 + 100);
		this.buttonArea.setX(getX());
		this.buttonArea.setY(getY());
		// nasty hacky code for options text
		if (isImageButton()) {
			if (buttonArea.contains(gc.getInput().getMouseX(), gc.getInput().getMouseY()))
				g.drawImage(new Image("resources/menu/" + buttonText + "_glow.png"), this.getX(), this.getY());
			else
				g.drawImage(new Image("resources/menu/" + buttonText + ".png"), this.getX(), this.getY());
		}
		else {
			g.setFont(gc.getDefaultFont());
			if (buttonArea.contains(gc.getInput().getMouseX(), gc.getInput().getMouseY()))
				g.drawRect(this.getX() - 20, this.getY() + getHeight() / 2 + 6, 10, 10);
			g.drawString(buttonText, this.getX(), this.getY() + getHeight() / 2);
		}
	}

    public void update (GameContainer gc) {
        buttonReleased = false;
        if (buttonArea.contains(gc.getInput().getMouseX(), gc.getInput().getMouseY())) {
            if (gc.getInput().isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
                buttonDown = true;
            }
            else {
                if (buttonDown) {
                    buttonDown = false;
                    buttonReleased = true;
                }
            }
        }
        else {
            buttonDown = false;
        }
    }

	public GameWindowContainer getContext() {
		return (GameWindowContainer) this.container;
	}

	public boolean isClicked() {
        if (buttonReleased) {
            buttonReleased = false;
            return true;
        }
        return false;
	}

	@Override
	public void setLocation(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	/***
	 * Returns the state change that corresponds to this button.
	 * @return
	 */
	public int getStateID(){
		return stateID;
	}

	public String getText() {
		return buttonText;
	}
	
	public int getIndex() {
		return index;
	}
	
	@Override
	public int getX() {
		return x;
	}

	@Override
	public int getY() {
		return y;
	}

	@Override
	public int getWidth() {
		return buttonWidth;
	}

	@Override
	public int getHeight() {
		return isImageButton() || buttonText.equals("Restart level") ? buttonHeight : textButtonHeight;
	}
	
	public boolean isImageButton() {
		return buttonText.equals("exit") || buttonText.equals("new_game") || buttonText.equals("options") || buttonText.equals("resume");
	}

	public final static int buttonWidth = 300;
	public final static int buttonHeight = 100;
	public final static int textButtonHeight = 50;

	public void setText(String string) {
		this.buttonText = string;
	}
}
